<?php
class Shop extends CI_Controller{
    
    public function index(){    
		$this->load->model('Shop_model');
		
		$code = '';
        if ($this->input->post('document_code')) {
            $code = $this->input->post('document_code');
        }
		
		$vars['document_code'] = $code;
        $vars['shop_list'] = $this->Shop_model->search($code);
        $vars['menu'] = 'shop';
        $vars['content_view'] = 'shop_search';
         
        $this->load->view('template', $vars);
    }
	
    public function edit($shop_id){
	    $this->load->model('Company_model');
		$this->load->model('Shop_model');
		$this->load->model('Shop_kardex_model');
		
		$shop = $this->Shop_model->findById($shop_id);
		$datei = $shop->date;
		$date = str_replace('-', '/', $datei);
		$datef = date("d/m/Y", strtotime($date) );
		$shop->date = $datef;
		
		$vars['form_title'] = 'Editar Guía de Compra';
		$vars['shop'] = $shop;
		$vars['company'] = $this->Company_model->getAll();
        $vars['shop_details'] = $this->Shop_model->findDetails($shop_id);
		$vars['warehouse_distribution'] = $this->Shop_kardex_model->findByShopId($shop_id);
		$vars['content_view'] = 'shop_form';
		$vars['menu'] = 'shop';
        $this->load->view('template', $vars);
	}
	
    public function add() {
        $this->load->model('Company_model');
		
		$vars['form_title'] = 'Nueva Guía de Compra';
		$vars['doc_date'] = date('d/m/Y');
		$vars['company'] = $this->Company_model->getAll();
        $vars['content_view'] = 'shop_form';
        $vars['menu'] = 'shop';
 
        $this->load->view('template', $vars);
    }
    
    public function products() {
        $code = null;
        $description = null;
        $this->load->model('Products_model');
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        $products = array();
        if ($code != null || $description != null) {
            $products = $this->Products_model->search($code, $description);
        }
        $vars['products'] = $products;
        $vars['code'] = $code;
        $vars['description'] = $description;
        $this->load->view('products_list', $vars);
		//$this->load->view('products_list_sale_shop', $vars);
    }
	
	public function stocks() {
        $code = null;
		
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        
        $this->load->model('warehouses_model');
        $this->load->model('Products_model');
        
		$vars['warehouses'] = $this->warehouses_model->getAll();
        $vars['productData'] = $this->Products_model->find($code);
        $this->load->view('stock_list_shop', $vars);
    }
	
	public function submit(){
		$this->load->model('Shop_model');
		$this->load->model('Warehouses_product_model');
		$this->load->model('Shop_kardex_model');
		
		$datei = $this->input->post('date');
		$date = str_replace('/', '-', $datei);
		$datef = date("Y-m-d", strtotime($date) ); 
              
		$data = array('document_type_id' => 3,
					'document_code' => $this->input->post('numero'),
					'date' => $datef,
					'id_supplier' =>  $this->input->post('supplier'),
					'observations' => $this->input->post('observations'),
					'amount' => $this->input->post('subtotalgeneral')
					);
       
	   $shop_id = $this->input->post('shop_id');
		
		if($shop_id){
			$warehouse_distribution = $this->Shop_kardex_model->findByShopId($shop_id);
			foreach ($warehouse_distribution as $distribution) {
				$this->Warehouses_product_model->remove($distribution->product_id,
													$distribution->id_warehouse,
													$distribution->box,
													$distribution->pack,
													$distribution->unit);      
			}
			$this->Shop_kardex_model->deleteAll($shop_id);
			$this->Shop_model->deleteAllDetails($shop_id);
			$this->Shop_model->update_shop($shop_id, $data);
		}
		else{
			$shop_id = $this->Shop_model->add_shop($data);
		}
		
		$product_ids_detalle = $this->input->post('product_id_detalle');
		$total_cajas = $this->input->post('total_cajas');
		$total_paquetes = $this->input->post('total_paquetes');
		$total_unidades = $this->input->post('total_unidades');
		$total_cost = $this->input->post('total_cost');
		$total_subtotalcost = $this->input->post('total_subtotalcost');
		
		for($i = 0 ; $i < count($product_ids_detalle); $i++){
               
			$data = array('shop_id' => $shop_id,
						 'product_id' => $product_ids_detalle[$i],
						 'box' => $total_cajas[$i],  
						 'pack' => $total_paquetes[$i],
						 'unit' => $total_unidades[$i],  
						 'Val_Unit' => $total_cost[$i], 
						 'amount' => $total_subtotalcost[$i], 
						);
						
            $this->Shop_model->add_shop_dateils($data);
		}
		
		$almacen_ids = $this->input->post('id_almacen');
		$product_id_almacenes = $this->input->post('product_id_almacenes');
		$cajas_de_almacen = $this->input->post('cajas_de_almacen');
		$paquetes_de_almacen = $this->input->post('paquetes_de_almacen');
		$unidades_de_almacen = $this->input->post('unidades_de_almacen');
		
		for($i = 0 ; $i < count($almacen_ids); $i++){
			$data1 = array('shop_id' => $shop_id,
							'product_id' => $product_id_almacenes[$i],
							'box' => $cajas_de_almacen[$i],
							'pack' => $paquetes_de_almacen[$i],
							'unit' => $unidades_de_almacen[$i],
							'id_warehouse' => $almacen_ids[$i]);
            $this->Shop_kardex_model->addKardexShop($data1);
        
		
			$productos_en_almacenes = $this->Shop_model->select_almacen_productos($product_id_almacenes[$i],$almacen_ids[$i]);
		
			$updateflag = count($productos_en_almacenes) > 0;
         
			$this->Warehouses_product_model->add($product_id_almacenes[$i],
													$almacen_ids[$i],
													$cajas_de_almacen[$i],
													$paquetes_de_almacen[$i],
													$unidades_de_almacen[$i],
													$updateflag);                   
		}
		
		$this->edit($shop_id); 
	}
        		
	public function search_code_unique() {
		$this->load->model('Shop_model');
		$document_code = $this->input->post('numero');

		echo $this->Shop_model->comparaction_code($document_code);
		return false;
	}
}
?>
